package com.carsite.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import org.acegisecurity.GrantedAuthority;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;


/**
 * This class is used to represent available roles in the database.</p>
 *
 * <p><a href="Role.java.html"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 *  Version by Dan Kibler dan@getrolling.com
 *  Extended to implement Acegi GrantedAuthority interface 
 *  	by David Carter david@carter.net
 *
 * @struts.form extends="BaseForm"
 * @hibernate.class table="role"
 */
public class Role extends BaseObject implements Serializable, GrantedAuthority {
    
	private static final long serialVersionUID = 3690197650654049848L;
    
	private Long id;
    private String name;
    private String cnName;          //中文名
    
    private String description;
    private String levels;
    
    

    protected Set privileges = new HashSet();
    
    

    public Role() {}
    
    public Role(String name) {
        this.name = name;
    }
    
    /**
     * @hibernate.set table="rolePrivilege" cascade="save-update" lazy="false"
     * @hibernate.collection-key column="role_id"
     * @hibernate.collection-many-to-many class="com.carsite.model.ModuleMenu" column="menu_id"
     */
    public Set getPrivileges() {
        return privileges;
    }
    
    /**
     *  @struts.form-field form-name="RoleForm"
     * @hibernate.id column="id" generator-class="increment" unsaved-value="null"
     */
    public Long getId() {
        return id;
    }

    /**
     * @see org.acegisecurity.GrantedAuthority#getAuthority()
     */
    public String getAuthority() {
        return getName();
    }
    
    /**
     *  @struts.form-field form-name="RoleForm"
     * @hibernate.property column="name" length="20"
     */
    public String getName() {
        return this.name;
    }
    
    /**
     * @struts.validator type="required"
	 * @struts.form-field form-name="RoleForm"
     * @hibernate.property column="cnName" length="30"
     */
	public String getCnName() {
		return cnName;
	}
	
	/**
     * @struts.form-field form-name="RoleForm"
     * @hibernate.property column="levels" length="30"
     */
    public String getLevels() {
		return levels;
	}
    
    /**
     * @struts.form-field form-name="RoleForm"
     * @hibernate.property column="description" length="200"
     */
    public String getDescription() {
        return this.description;
    }
    
	public void addPrivilege(ModuleMenu menu) {
		getPrivileges().add(menu);
    }
	
	/**
	 * @param privileges The privileges to set.
	 */
	public void setPrivileges(Set privileges) {
		this.privileges = privileges;
	}
    
	public void setCnName(String cnName) {
		this.cnName = cnName;
	}
	public void setLevels(String levels) {
		this.levels = levels;
	}
    public void setId(Long id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Role)) return false;

        final Role role = (Role) o;

        return !(name != null ? !name.equals(role.name) : role.name != null);

    }

    public int hashCode() {
        return (name != null ? name.hashCode() : 0);
    }

    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE)
                .append(this.name)
                .toString();
    }

}
